package com.wukong.util;

import java.sql.*;

import static java.sql.DriverManager.getConnection;

public class jdbcUtil {
    //连接数据库
    /**
     * 4个要素
     * driver
     * url
     * 用户名
     * 密码
     */
    public static void connectMysql(){
        String driver ="com.mysql.cj.jdbc.Driver";
        String  url = "jdbc:mysql://localhost:3306/qq_db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
        String user ="root";
        String password ="USj;_!po7+Dz";

        try {
            //加载驱动
            Class.forName(driver);
            //获得连接对象java.sql.Connection
            Connection connection = getConnection(url, user, password);
//            //查询数据库信息中信息
//            String sql="SELECT id  FROM `qq_user`";
            //获得查询结果集
//            ResultSet resultSet = preparedStatement.executeQuery();
            //对结果集进行遍历
//            int id = 0;
//            while(resultSet.next()){
//                id = resultSet.getInt(1); //2
//            }
//            System.out.println("学号为002同学的id为什么？"); //2

            //需要执行的sql语句（?是占位符，代表一个参数
            String sql="insert into qq_user(id,username,password,email) values(?,?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            //给sql语句中的 占位符？ 赋值
            preparedStatement.setInt(1,7);
            preparedStatement.setString(2,"zs");
            preparedStatement.setString(3,"007");
            preparedStatement.setString(4,"007@qq.com");
            //获得插入结果集
            boolean resultSet = preparedStatement.execute();
            //对结果集进行遍历
            System.out.println(resultSet);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        connectMysql();
    }

}
